package com.khshop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.khshop.pojo.Order;
import com.khshop.service.OrderService;
import com.khshop.vo.AjaxResult;
import com.khshop.vo.ServiceResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-15
 */
@Api(tags = "订单 前端控制器")
@Controller
@RequestMapping("/")
public class OrderController {

    @Autowired
    OrderService orderService;

    //根据订单状态【待付款 待发货 待收货 待评价】查询订单信息
    @ApiOperation("根据订单状态【待付款 待发货 待收货 待评价】查询订单信息")
    @PostMapping("/searchOrderByStatus")
    @ResponseBody
    public AjaxResult searchOrderByStatus(@RequestParam("status") Integer status,
                                          @RequestParam(value = "page",defaultValue = "1") Integer page,
                                          @RequestParam(value = "limit",defaultValue = "15") Integer limit){
        ServiceResult<IPage<Order>> serviceResult = orderService.searchOrderByStatus(status, page, limit);
        if (serviceResult.isFlag()) {
            return  AjaxResult.success("修改成功", (int) serviceResult.getData().getTotal(),serviceResult.getData());
        } else {
            return  AjaxResult.error("修改失败，请稍后重试！");
        }
    }

    //下单与微信支付接口对接
    @ApiOperation("下单与微信支付接口对接")
    @PostMapping("/addOrder")
    @ResponseBody
    public AjaxResult addOrder(@RequestBody Order order){
        if (orderService.addOrder(order).isFlag()) {
            return  AjaxResult.success("下单成功");
        } else {
            return  AjaxResult.error("下单失败，请稍后重试！");
        }
    }


    //未支付订单处理
    @ApiOperation("未支付订单处理")
    @PostMapping("/closeOrder")
    @ResponseBody
    public AjaxResult closeOrder(Long id){
        if (orderService.closeOrder(id).isFlag()) {
            return  AjaxResult.success("处理成功");
        } else {
            return  AjaxResult.error("处理失败，请稍后重试！");
        }
    }
    //取消订单处理
    @ApiOperation("取消订单处理")
    @PostMapping("/cancelOrder")
    @ResponseBody
    public AjaxResult cancelOrder(Long id){
        if (orderService.cancelOrder(id).isFlag()) {
            return  AjaxResult.success("取消成功");
        } else {
            return  AjaxResult.error("取消失败，请稍后重试！");
        }
    }
    //取消订单微信退款实现
    @ApiOperation("取消订单微信退款实现")
    @PostMapping("/cancelAndReturnOrder")
    @ResponseBody
    public AjaxResult cancelAndReturnOrder(Long id){
        if (orderService.cancelAndReturnOrder(id).isFlag()) {
            return  AjaxResult.success("取消成功");
        } else {
            return  AjaxResult.error("取消失败，请稍后重试！");
        }
    }
    //改变订单状态
    @ApiOperation("改变订单状态")
    @PostMapping("/changeOrderStatus")
    @ResponseBody
    public AjaxResult changeOrderStatus(Long id, Integer status){
        if (orderService.changeOrderStatus(id, status).isFlag()) {
            return  AjaxResult.success("改变成功");
        } else {
            return  AjaxResult.error("改变失败，请稍后重试！");
        }
    }
}

